/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 3 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

package org.systemsbiology.iteration;

import java.util.Iterator;

/**
 * Author: anorberg
 * Created on: Nov 4, 2010
 */

/**
 * Wrapper class to prevent anybody from calling remove() on an iterator returned from an object intended to be
 * immutable but relying on an iterator from one of its internal data structures to expose data. 
 * @param <E> Element type of the iterator to wrap. Must be the same as the iterator's element type.
 */
public class BlockRemoveFromIterator<E> implements Iterator<E> {

	/**
	 * Iterator being wrapped.
	 */
    private Iterator<E> backingIterator;
    
    /**
     * Constructs a BlockRemoveFromIterator- a view of an Iterator with the remove() method disabled,
     * so an iterator that otherwise allows remove() can safely be returned as a value without
     * breaking hard encapsulation guarantees.
     * @param iter
     */
    public BlockRemoveFromIterator(Iterator<E> iter){
        if(iter == null){
            throw new NullPointerException("BlockRemoveFromIterator needs an underlying iterator to wrap");
        }
        backingIterator = iter;
    }

    public boolean hasNext(){
        return backingIterator.hasNext();
    }

    public E next(){
        return backingIterator.next();
    }

    /**
     * Throws an UnsupportedOperationException.
     * @throws UnsupportedOperationException - always, because the entire point of this class is that remove() doesn't work
     */
    public void remove(){
        throw new UnsupportedOperationException("BlockRemoveFromIterator used to intentionally prevent remove() from working.");
    }

}
